package cn.student.service.impl;

import cn.student.config.web.DataGridResultInfo;
import cn.student.dao.WorkDaoMapper;
import cn.student.pojo.Work;
import cn.student.pojo.dto.StudentDTO;
import cn.student.service.WorkService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 毛苏宇
 * User: me
 * Date: 2020/6/26
 * Time: 17:15
 * Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WorkServiceImpl implements WorkService {

    @Autowired
    private WorkDaoMapper workDaoMapper;

    @Override
    public boolean add(Work work) {
        work.setUpdateTime(System.currentTimeMillis());
        work.setCreateTime(System.currentTimeMillis());
        return workDaoMapper.insert(work) > 0 ? true : false;
    }

    @Override
    public boolean remove(long workId) {
        return workDaoMapper.delete(workId) > 0 ? true : false;
    }

    @Override
    public DataGridResultInfo<Work> find(int page, int rows) {
        Page<Work> works = PageHelper.startPage(page, rows);
        workDaoMapper.select();
        DataGridResultInfo<Work> dataGridResultInfo = new DataGridResultInfo<>();
        dataGridResultInfo.setEndRow(works.getEndRow());
        dataGridResultInfo.setPageNum(works.getPageNum());
        dataGridResultInfo.setPages(works.getPages());
        dataGridResultInfo.setPageSize(works.getPageSize());
        dataGridResultInfo.setRows(works.getResult());
        dataGridResultInfo.setStartRow(works.getStartRow());
        dataGridResultInfo.setTotal(works.getTotal());
        return dataGridResultInfo;
    }

    @Override
    public Work findById(long workId) {
        return workDaoMapper.selectById(workId);
    }

    @Override
    public boolean modify(Work work) {
        work.setUpdateTime(System.currentTimeMillis());
        return workDaoMapper.update(work) > 0 ? true : false;
    }

    @Override
    public Work findByUserId(long userId, long cpId) {
        return workDaoMapper.selectByUserId(userId, cpId);
    }
}
